home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / des_c.exe / DES.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-14  |  13.5 KB  |  528 lines

  1. /* Sofware DES functions
  2.  * written 12 Dec 1986 by Phil Karn, KA9Q; large sections adapted from
  3.  * the 1977 public-domain program by Jim Gillogly
  4.  */
  5.  
  6. #ifdef __TURBOC__
  7. #include <stdio.h>
  8. #include <alloc.h>
  9. #define LITTLE_ENDIAN
  10. #else
  11. #define    NULL    0
  12. #endif
  13.  
  14. #ifdef    LITTLE_ENDIAN
  15. unsigned long byteswap();
  16. #endif
  17.  
  18. /* Tables defined in the Data Encryption Standard documents */
  19.  
  20. /* initial permutation IP */
  21. static char ip[] = {
  22.     58, 50, 42, 34, 26, 18, 10,  2,
  23.     60, 52, 44, 36, 28, 20, 12,  4,
  24.     62, 54, 46, 38, 30, 22, 14,  6,
  25.     64, 56, 48, 40, 32, 24, 16,  8,
  26.     57, 49, 41, 33, 25, 17,  9,  1,
  27.     59, 51, 43, 35, 27, 19, 11,  3,
  28.     61, 53, 45, 37, 29, 21, 13,  5,
  29.     63, 55, 47, 39, 31, 23, 15,  7
  30. };
  31.  
  32. /* final permutation IP^-1 */
  33. static char fp[] = {
  34.     40,  8, 48, 16, 56, 24, 64, 32,
  35.     39,  7, 47, 15, 55, 23, 63, 31,
  36.     38,  6, 46, 14, 54, 22, 62, 30,
  37.     37,  5, 45, 13, 53, 21, 61, 29,
  38.     36,  4, 44, 12, 52, 20, 60, 28,
  39.     35,  3, 43, 11, 51, 19, 59, 27,
  40.     34,  2, 42, 10, 50, 18, 58, 26,
  41.     33,  1, 41,  9, 49, 17, 57, 25
  42. };
  43.  
  44. /* expansion operation matrix
  45.  * This is for reference only; it is unused in the code
  46.  * as the f() function performs it implicitly for speed
  47.  */
  48. #ifdef notdef
  49. static char ei[] = {
  50.     32,  1,  2,  3,  4,  5,
  51.      4,  5,  6,  7,  8,  9,
  52.      8,  9, 10, 11, 12, 13,
  53.     12, 13, 14, 15, 16, 17,
  54.     16, 17, 18, 19, 20, 21,
  55.     20, 21, 22, 23, 24, 25,
  56.     24, 25, 26, 27, 28, 29,
  57.     28, 29, 30, 31, 32,  1 
  58. };
  59. #endif
  60.  
  61. /* permuted choice table (key) */
  62. static char pc1[] = {
  63.     57, 49, 41, 33, 25, 17,  9,
  64.      1, 58, 50, 42, 34, 26, 18,
  65.     10,  2, 59, 51, 43, 35, 27,
  66.     19, 11,  3, 60, 52, 44, 36,
  67.  
  68.     63, 55, 47, 39, 31, 23, 15,
  69.      7, 62, 54, 46, 38, 30, 22,
  70.     14,  6, 61, 53, 45, 37, 29,
  71.     21, 13,  5, 28, 20, 12,  4
  72. };
  73.  
  74. /* number left rotations of pc1 */
  75. static char totrot[] = {
  76.     1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
  77. };
  78.  
  79. /* permuted choice key (table) */
  80. static char pc2[] = {
  81.     14, 17, 11, 24,  1,  5,
  82.      3, 28, 15,  6, 21, 10,
  83.     23, 19, 12,  4, 26,  8,
  84.     16,  7, 27, 20, 13,  2,
  85.     41, 52, 31, 37, 47, 55,
  86.     30, 40, 51, 45, 33, 48,
  87.     44, 49, 39, 56, 34, 53,
  88.     46, 42, 50, 36, 29, 32
  89. };
  90.  
  91. /* The (in)famous S-boxes */
  92. static char si[8][64] = {
  93.     /* S1 */
  94.     14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
  95.      0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
  96.      4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
  97.     15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13,
  98.  
  99.     /* S2 */
  100.     15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
  101.      3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
  102.      0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
  103.     13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9,
  104.  
  105.     /* S3 */
  106.     10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
  107.     13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
  108.     13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
  109.      1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12,
  110.  
  111.     /* S4 */
  112.      7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
  113.     13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
  114.     10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
  115.      3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14,
  116.  
  117.     /* S5 */
  118.      2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
  119.     14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
  120.      4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
  121.     11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3,
  122.  
  123.     /* S6 */
  124.     12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
  125.     10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
  126.      9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
  127.      4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13,
  128.  
  129.     /* S7 */
  130.      4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
  131.     13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
  132.      1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
  133.      6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12,
  134.  
  135.     /* S8 */
  136.     13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
  137.      1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
  138.      7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
  139.      2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11
  140. };
  141.  
  142. /* 32-bit permutation function P used on the output of the S-boxes */
  143. static char p32i[] = {    
  144.     16,  7, 20, 21,
  145.     29, 12, 28, 17,
  146.      1, 15, 23, 26,
  147.      5, 18, 31, 10,
  148.      2,  8, 24, 14,
  149.     32, 27,  3,  9,
  150.     19, 13, 30,  6,
  151.     22, 11,  4, 25
  152. };
  153. /* End of DES-defined tables */
  154.  
  155. /* Lookup tables initialized once only at startup by desinit() */
  156. static long (*sp)[64];        /* Combined S and P boxes */
  157.  
  158. static char (*iperm)[16][8];    /* Initial and final permutations */
  159. static char (*fperm)[16][8];
  160.  
  161. /* 8 6-bit subkeys for each of 16 rounds, initialized by setkey() */
  162. static unsigned char (*kn)[8];
  163.  
  164. /* bit 0 is left-most in byte */
  165. static int bytebit[] = {
  166.     0200,0100,040,020,010,04,02,01
  167. };
  168.  
  169. static int nibblebit[] = {
  170.      010,04,02,01
  171. };
  172. static int desmode;
  173.  
  174. /* Allocate space and initialize DES lookup arrays
  175.  * mode == 0: standard Data Encryption Algorithm
  176.  * mode == 1: DEA without initial and final permutations for speed
  177.  * mode == 2: DEA without permutations and with 128-byte key (completely
  178.  *            independent subkeys for each round)
  179.  */
  180. desinit(mode)
  181. int mode;
  182. {
  183. #ifndef __TURBOC__
  184.     char *malloc();
  185. #endif
  186.  
  187.     if(sp != NULL){
  188.         /* Already initialized */
  189.         return 0;
  190.     }
  191.     desmode = mode;
  192.     
  193.     if((sp = (long (*)[64])malloc(sizeof(long) * 8 * 64)) == NULL){
  194.         return -1;
  195.     }
  196.     spinit();
  197.     kn = (unsigned char (*)[8])malloc(sizeof(char) * 8 * 16);
  198.     if(kn == NULL){
  199.         free((char *)sp);
  200.         return -1;
  201.     }
  202.     if(mode == 1 || mode == 2)    /* No permutations */
  203.         return 0;
  204.  
  205.     iperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
  206.     if(iperm == NULL){
  207.         free((char *)sp);
  208.         free((char *)kn);
  209.         return -1;
  210.     }
  211.     perminit(iperm,ip);
  212.  
  213.     fperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
  214.     if(fperm == NULL){
  215.         free((char *)sp);
  216.         free((char *)kn);
  217.         free((char *)iperm);
  218.         return -1;
  219.     }
  220.     perminit(fperm,fp);
  221.     
  222.     return 0;
  223. }
  224. /* Free up storage used by DES */
  225. desdone()
  226. {
  227.     if(sp == NULL)
  228.         return;    /* Already done */
  229.  
  230.     free((char *)sp);
  231.     free((char *)kn);
  232.     if(iperm != NULL)
  233.         free((char *)iperm);
  234.     if(fperm != NULL)
  235.         free((char *)fperm);
  236.  
  237.     sp = NULL;
  238.     iperm = NULL;
  239.     fperm = NULL;
  240.     kn = NULL;
  241. }
  242. /* Set key (initialize key schedule array) */
  243. setkey(key)
  244. char *key;            /* 64 bits (will use only 56) */
  245. {
  246.     char pc1m[56];        /* place to modify pc1 into */
  247.     char pcr[56];        /* place to rotate pc1 into */
  248.     register int i,j,l;
  249.     int m;
  250.  
  251.     /* In mode 2, the 128 bytes of subkey are set directly from the
  252.      * user's key, allowing him to use completely independent
  253.      * subkeys for each round. Note that the user MUST specify a
  254.      * full 128 bytes.
  255.      *
  256.      * I would like to think that this technique gives the NSA a real
  257.      * headache, but I'm not THAT naive.
  258.      */
  259.     if(desmode == 2){
  260.         for(i=0;i<16;i++)
  261.             for(j=0;j<8;j++)
  262.                 kn[i][j] = *key++;
  263.         return;
  264.     }
  265.     /* Clear key schedule */
  266.     for (i=0; i<16; i++)
  267.         for (j=0; j<8; j++)
  268.             kn[i][j]=0;
  269.  
  270.     for (j=0; j<56; j++) {        /* convert pc1 to bits of key */
  271.         l=pc1[j]-1;        /* integer bit location     */
  272.         m = l & 07;        /* find bit         */
  273.         pc1m[j]=(key[l>>3] &    /* find which key byte l is in */
  274.             bytebit[m])    /* and which bit of that byte */
  275.             ? 1 : 0;    /* and store 1-bit result */
  276.     }
  277.     for (i=0; i<16; i++) {        /* key chunk for each iteration */
  278.         for (j=0; j<56; j++)    /* rotate pc1 the right amount */
  279.             pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];
  280.             /* rotate left and right halves independently */
  281.         for (j=0; j<48; j++){    /* select bits individually */
  282.             /* check bit that goes to kn[j] */
  283.             if (pcr[pc2[j]-1]){
  284.                 /* mask it in if it's there */
  285.                 l= j % 6;
  286.                 kn[i][j/6] |= bytebit[l] >> 2;
  287.             }
  288.         }
  289.     }
  290. }
  291. /* In-place encryption of 64-bit block */
  292. endes(block)
  293. char *block;
  294. {
  295.     register int i;
  296.     unsigned long work[2];         /* Working data storage */
  297.     long tmp;
  298.  
  299.     permute(block,iperm,(char *)work);    /* Initial Permutation */
  300. #ifdef LITTLE_ENDIAN
  301.     work[0] = byteswap(work[0]);
  302.     work[1] = byteswap(work[1]);
  303. #endif
  304.  
  305.     /* Do the 16 rounds */
  306.     for (i=0; i<16; i++)
  307.         round(i,work);
  308.  
  309.     /* Left/right half swap */
  310.     tmp = work[0];
  311.     work[0] = work[1];    
  312.     work[1] = tmp;
  313.  
  314. #ifdef LITTLE_ENDIAN
  315.     work[0] = byteswap(work[0]);
  316.     work[1] = byteswap(work[1]);
  317. #endif
  318.     permute((char *)work,fperm,block);    /* Inverse initial permutation */
  319. }
  320. /* In-place decryption of 64-bit block */
  321. dedes(block)
  322. char *block;
  323. {
  324.     register int i;
  325.     unsigned long work[2];    /* Working data storage */
  326.     long tmp;
  327.  
  328.     permute(block,iperm,(char *)work);    /* Initial permutation */
  329.  
  330. #ifdef LITTLE_ENDIAN
  331.     work[0] = byteswap(work[0]);
  332.     work[1] = byteswap(work[1]);
  333. #endif
  334.  
  335.     /* Left/right half swap */
  336.     tmp = work[0];
  337.     work[0] = work[1];    
  338.     work[1] = tmp;
  339.  
  340.     /* Do the 16 rounds in reverse order */
  341.     for (i=15; i >= 0; i--)
  342.         round(i,work);
  343.  
  344. #ifdef LITTLE_ENDIAN
  345.     work[0] = byteswap(work[0]);
  346.     work[1] = byteswap(work[1]);
  347. #endif
  348.  
  349.     permute((char *)work,fperm,block);    /* Inverse initial permutation */
  350. }
  351.  
  352. /* Permute inblock with perm */
  353. static
  354. permute(inblock,perm,outblock)
  355. char *inblock, *outblock;        /* result into outblock,64 bits */
  356. char perm[16][16][8];            /* 2K bytes defining perm. */
  357. {
  358.     register int i,j;
  359.     register char *ib, *ob;        /* ptr to input or output block */
  360.     register char *p, *q;
  361.  
  362.     if(perm == NULL){
  363.         /* No permutation, just copy */
  364.         for(i=8; i!=0; i--)
  365.             *outblock++ = *inblock++;
  366.         return;
  367.     }
  368.     /* Clear output block     */
  369.     for (i=8, ob = outblock; i != 0; i--)
  370.         *ob++ = 0;
  371.  
  372.     ib = inblock;
  373.     for (j = 0; j < 16; j += 2, ib++) { /* for each input nibble */
  374.         ob = outblock;
  375.         p = perm[j][(*ib >> 4) & 017];
  376.         q = perm[j + 1][*ib & 017];
  377.         for (i = 8; i != 0; i--){   /* and each output byte */
  378.             *ob++ |= *p++ | *q++;    /* OR the masks together*/
  379.         }
  380.     }
  381. }
  382.  
  383. /* Do one DES cipher round */
  384. static
  385. round(num,block)
  386. int num;                /* i.e. the num-th one     */
  387. unsigned long *block;
  388. {
  389.     long f();
  390.  
  391.     /* The rounds are numbered from 0 to 15. On even rounds
  392.      * the right half is fed to f() and the result exclusive-ORs
  393.      * the left half; on odd rounds the reverse is done.
  394.      */
  395.     if(num & 1){
  396.         block[1] ^= f(block[0],kn[num]);
  397.     } else {
  398.         block[0] ^= f(block[1],kn[num]);
  399.     }
  400. }
  401. /* The nonlinear function f(r,k), the heart of DES */
  402. static
  403. long
  404. f(r,subkey)
  405. unsigned long r;        /* 32 bits */
  406. unsigned char subkey[8];    /* 48-bit key for this round */
  407. {
  408.     register unsigned long rval,rt;
  409. #ifdef    TRACE
  410.     unsigned char *cp;
  411.     int i;
  412.  
  413.     printf("f(%08lx, %02x %02x %02x %02x %02x %02x %02x %02x) = ",
  414.         r,
  415.         subkey[0], subkey[1], subkey[2],
  416.         subkey[3], subkey[4], subkey[5],
  417.         subkey[6], subkey[7]);
  418. #endif
  419.     /* Run E(R) ^ K through the combined S & P boxes
  420.      * This code takes advantage of a convenient regularity in
  421.      * E, namely that each group of 6 bits in E(R) feeding
  422.      * a single S-box is a contiguous segment of R.
  423.      */
  424.     rt = (r >> 1) | ((r & 1) ? 0x80000000 : 0);
  425.     rval = 0;
  426.     rval |= sp[0][((rt >> 26) ^ *subkey++) & 0x3f];
  427.     rval |= sp[1][((rt >> 22) ^ *subkey++) & 0x3f];
  428.     rval |= sp[2][((rt >> 18) ^ *subkey++) & 0x3f];
  429.     rval |= sp[3][((rt >> 14) ^ *subkey++) & 0x3f];
  430.     rval |= sp[4][((rt >> 10) ^ *subkey++) & 0x3f];
  431.     rval |= sp[5][((rt >> 6) ^ *subkey++) & 0x3f];
  432.     rval |= sp[6][((rt >> 2) ^ *subkey++) & 0x3f];
  433.     rt = (r << 1) | ((r & 0x80000000) ? 1 : 0);
  434.     rval |= sp[7][(rt ^ *subkey) & 0x3f];
  435. #ifdef    TRACE
  436.     printf(" %08lx\n",rval);
  437. #endif
  438.     return rval;
  439. }
  440. /* initialize a perm array */
  441. static
  442. perminit(perm,p)
  443. char perm[16][16][8];            /* 64-bit, either init or final */
  444. char p[64];
  445. {
  446.     register int l, j, k;
  447.     int i,m;
  448.  
  449.     /* Clear the permutation array */
  450.     for (i=0; i<16; i++)
  451.         for (j=0; j<16; j++)
  452.             for (k=0; k<8; k++)
  453.                 perm[i][j][k]=0;
  454.  
  455.     for (i=0; i<16; i++)        /* each input nibble position */
  456.         for (j = 0; j < 16; j++)/* each possible input nibble */
  457.         for (k = 0; k < 64; k++)/* each output bit position */
  458.         {   l = p[k] - 1;    /* where does this bit come from*/
  459.             if ((l >> 2) != i)  /* does it come from input posn?*/
  460.             continue;    /* if not, bit k is 0     */
  461.             if (!(j & nibblebit[l & 3]))
  462.             continue;    /* any such bit in input? */
  463.             m = k & 07;    /* which bit is this in the byte*/
  464.             perm[i][j][k>>3] |= bytebit[m];
  465.         }
  466. }
  467.  
  468. /* Initialize the lookup table for the combined S and P boxes */
  469. static int
  470. spinit()
  471. {
  472.     char pbox[32];
  473.     int p,i,s,j,rowcol;
  474.     long val;
  475.  
  476.     /* Compute pbox, the inverse of p32i.
  477.      * This is easier to work with
  478.      */
  479.     for(p=0;p<32;p++){
  480.         for(i=0;i<32;i++){
  481.             if(p32i[i]-1 == p){
  482.                 pbox[p] = i;
  483.                 break;
  484.             }
  485.         }
  486.     }
  487.     for(s = 0; s < 8; s++){            /* For each S-box */
  488.         for(i=0; i<64; i++){        /* For each possible input */
  489.             val = 0;
  490.             /* The row number is formed from the first and last
  491.              * bits; the column number is from the middle 4
  492.              */
  493.             rowcol = (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) & 0xf);
  494.             for(j=0;j<4;j++){    /* For each output bit */
  495.                 if(si[s][rowcol] & (8 >> j)){
  496.                  val |= 1L << (31 - pbox[4*s + j]);
  497.                 }
  498.             }
  499.             sp[s][i] = val;
  500.  
  501. #ifdef    DEBUG
  502.             printf("sp[%d][%2d] = %08lx\n",s,i,sp[s][i]);
  503. #endif
  504.         }
  505.     }
  506. }
  507. #ifdef    LITTLE_ENDIAN
  508. /* Byte swap a long */
  509. static
  510. unsigned long
  511. byteswap(x)
  512. unsigned long x;
  513. {
  514.     register char *cp,tmp;
  515.  
  516.     cp = (char *)&x;
  517.     tmp = cp[3];
  518.     cp[3] = cp[0];
  519.     cp[0] = tmp;
  520.  
  521.     tmp = cp[2];
  522.     cp[2] = cp[1];
  523.     cp[1] = tmp;
  524.  
  525.     return x;
  526. }
  527. #endif
  528.